/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tok.predmeta;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author mega
 */
class Separate implements Runnable {

    public Separate() {
        Reminder r = new Reminder();
        Calendar cal1 = new GregorianCalendar();
        Calendar cal2 = new GregorianCalendar();
        Calendar cal22 = new GregorianCalendar();
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        String month = String.valueOf(gregorianCalendar.get(GregorianCalendar.MONTH));
        int Month = Integer.parseInt(month);
        String day = String.valueOf(gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH));

        int Day = Integer.parseInt(day);
        String year = String.valueOf(gregorianCalendar.get(GregorianCalendar.YEAR));
        int Year = Integer.parseInt(year);




        // Load the JDBC driver
       

// Create a connection to the database
        String serverName = "192.168.88.10";
        String mydatabase = "tok_predmeta";
        String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?useUnicode=true&characterEncoding=utf8"; // a JDBC url
        String username = "tokpred";
        String password = "Lx3sAQdnYJFTxBhs";

        try {
             java.sql.Connection connection = DriverManager.getConnection(url, username, password);
             java.sql.PreparedStatement s = connection.prepareStatement("SELECT *  FROM predmeti");

            s.executeQuery();


            ResultSet rs = s.getResultSet();

            ArrayList al = new ArrayList();
            int count = 0;
            while (rs.next()) {
                int idNum = rs.getInt("id");
                String sifra = rs.getString("sifra");
                 String korisnicka_sifra = rs.getString("korisnicka_sifra");
                String ime = rs.getString("ime");
                String prezime = rs.getString("prezime");
                String adresa = rs.getString("adresa");
                String jmbg = rs.getString("jmbg");
                String tip = rs.getString("tip");
                String datum = rs.getString("datum");
                //  String datum2 = rs.getString("date");
                String[] datum_predmeta = datum.split("/");



                int Dan = Integer.parseInt(datum_predmeta[0]);
                int Mesec = Integer.parseInt(datum_predmeta[1]) - 1;
                int Godina = Integer.parseInt(datum_predmeta[2]);
                int broj_dana2 = r.daysBetween(cal22.getTime(), cal1.getTime());
                broj_dana2 = Math.abs(broj_dana2);
                cal1.set(Year, Month, Day);
                cal2.set(Godina, Mesec, Dan);

                int broj_dana = r.daysBetween(cal2.getTime(), cal1.getTime());
                broj_dana = Math.abs(broj_dana);
               
                if (broj_dana > 5 && broj_dana < 20 ) {
                    java.sql.PreparedStatement su = connection.prepareStatement("DELETE * from predmeti2");
                    su.executeQuery();
                    java.sql.PreparedStatement e = connection.prepareStatement("INSERT INTO state (sifra,korisnicka_sifra,ime,prezime,adresa,jmbg,tip,datum,korisnik) VALUES "
                                    + "(?,?,?,?,?,?,?,?,?)");

                            e.setString(1, sifra);
                            e.setString(2, korisnicka_sifra);
                            e.setString(3, ime);
                            e.setString(4, prezime);
                            e.setString(5, adresa);
                            e.setString(6, jmbg);
                            e.setString(7, tip);
                            e.setString(8, datum);
                             e.setString(9, Login.user);
                            e.executeUpdate();

                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(Pretraga.class.getName()).log(Level.SEVERE, null, ex);

        }
    

    }

    @Override
    public void run() {
        
        
    }

}
